package Aleehot100;

/**
 * ClassName: c050
 * Package: Aleehot100
 * Description: 124. 二叉树中的最大路径和
 *
 * @Author BCXJ
 * @Create 2025/7/30 08:46
 * @Version 1.0
 * @Since 1.0
 */
public class c050 {
    public static void main(String[] args) {
        TreeNode treeNode = new TreeNode(-3);
        System.out.println(maxPathSum(treeNode));
    }
    static class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode() {
        }

        TreeNode(int val) {
            this.val = val;
        }

        TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }

    static int res = Integer.MIN_VALUE;
    public static int maxPathSum(TreeNode root) {
        res = 0;
        process(root);
        return res;
    }

    private static int process(TreeNode root) {
        if(root == null) {
            return 0;
        }
        int l = Math.max(0, process(root.left));
        int r = Math.max(0, process(root.right));
        res = Math.max(l + r + root.val, res);
        return Math.max(l , r) + root.val;
    }

}
